<template>
  <comcard>
    <div slot="title_name">修改密码</div>
    <div slot="body_name">
      <el-form :model="user" label-width="100px" :rules="checkuser" ref="upd">
        <el-form-item label="原密码" prop="account">
          <el-row>
            <el-col :span="6">
              <el-input type="text" v-model="user.account"></el-input
            ></el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-row>
            <el-col :span="6"
              ><el-input
                show-password
                type="password"
                v-model="user.password"
                class="bb"
              ></el-input
            ></el-col>
          </el-row>
        </el-form-item>

        <el-form-item label="确认密码" prop="comPwd">
          <el-row>
            <el-col :span="6"
              ><el-input type="password" v-model="user.comPwd"></el-input
            ></el-col>
          </el-row>
        </el-form-item>

        <el-form-item>
          <el-button type="danger" @click="md()">确认</el-button>
        </el-form-item>
      </el-form>
    </div>
  </comcard>
</template>

<script>
import { api_newPwd, api_old } from "../../api/users";
import comcard from "../../components/comcard/index.vue";

export default {
  components: {
    comcard,
  },
  data() {
    const checkoldpwd = async (rule, value, callback) => {
      let rst = await api_old({ oldPwd: value });
      console.log(rst);
      let { code, msg } = rst;
      if (code == "00") {
        callback();
      } else {
        return callback(new Error(msg));
      }
    };

    const checkNewPWd = async (rule, value, callback) => {
      let rst = await api_newPwd({ newPwd: value });
      console.log(rst);
      let { code, msg } = rst;
      if ((code, msg)) {
        callback();
      } else {
        return callback(new Error(msg));
      }
    };

    const checkcomPWd = (rule, value, callback) => {
      if (value == this.user.password) {
        callback();
      } else {
        return callback(new Error("确认密码与新密码不一致！！"));
      }
    };
    return {
      user: {
        account: "",
        password: "",
        comPwd: "",
      },

      checkuser: {
        account: [{ validator: checkoldpwd, tigger: "blur" }],
        password: [{ validator: checkNewPWd, trigger: "blur" }],
        comPwd: [{ validator: checkcomPWd, trigger: "blur" }],
      },
    };
  },
  methods: {
    md() {
      this.$refs["upd"].validate(async (b) => {
        if (b) {
          let rst = await api_newPwd({ newPwd: this.user.password });
          console.log(rst);
          let { code, msg } = rst;
          if (code == 0) {
            this.logcal.remove("sell_token");
            this.$router.replace("/login");
          }
        } else {
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>